﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Data.Entity;
using System.Data.EntityClient;
using System.Data.EntityModel;
using DotaPick.Model;
using System.Runtime.Serialization.Json;
using System.IO;
using System.Text;


namespace DotaPick
{
    static class Program
    {
        /// <summary>
        /// Главная точка входа для приложения.
        /// </summary>
        [STAThread]
        static void Main()
        {
            ModelContext dbContext = new ModelContext();
            if (dbContext.Heroes.Count() == 0)
            {
                fillDatabase(dbContext);
            }
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new MainForm());
        }
        public static void fillDatabase(ModelContext dbContext)
        {
            FileStream fileStream = new FileStream("Heroes.js", FileMode.Open);
            StreamReader streamReader = new StreamReader(fileStream);
            MemoryStream memoryStream = new MemoryStream(Encoding.UTF8.GetBytes(streamReader.ReadToEnd()));
            DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(HeroesList));
            memoryStream.Position = 0;
            
            HeroesList heroesList = (HeroesList)serializer.ReadObject(memoryStream);
            foreach (Hero hero in heroesList.Heroes)
            {
                dbContext.Heroes.Add(hero);
                dbContext.SaveChanges();
            }
        }
    }
}
